$(document).ready(function() {
	queryUsers("",0,"");//首次默认加载
});

var queryUsers = function(condition,pageNo,loginName){
	$("#jigou").hide();//影藏当前页面数据
	$("#loadingtable").show();//数据加载进度提示
	var pageSize = 5;
	jQuery.post("/finance/user/queryUsers.do",{pageSize:pageSize,pageNo:pageNo,loginName:loginName},function(str){
			$("#loadingtable").hide();//隐藏数据加载进度提示
			$("#jigou").show();//影藏当前页面数据
			//拼表头
			var htmltxtHead = "<caption>用户信息</caption>"
								+'<thead>'
								+'<tr>'
								+'<th width="50">序号</th>'
								+'<th width="75">登录名</th>'
								+'<th width="70">姓名</th>'
								+'<th width="100">职位名称</th>'
								+'<th width="100">公司名称</th>'
								+'<th width="70">创建时间</th>'
								+'<th width="70">更新时间</th>'
								+'<th width="70">用户类型</th>'
								+'<th width="70">是否有效</th>'
								+'<th width="175">操作</th>'
								+'</tr>'
								+'</thead>'
			var htmltxtcontent = "";	
			var data = eval('(' + str + ')');
			htmltxtcontent += '<tbody>';
			if(data.data.length == 0){
				 htmltxtcontent += '<tr>'
				 				   +'<td width="50" colspan="10" align="center">对不起,没有您要查询的数据!</td>'
						         +'</tr>';
			}
			for (var i=0; i<data.data.length; i++) {
				 htmltxtcontent += '<tr>'
				 				   +'<td width="50" align="center">'+ (i+1) +'</td>'
				 				   +'<td width="75" align="center">'+ data.data[i].loginName +'</td>'
				 				   +'<td width="70" align="center">'+ data.data[i].cNName +'</td>'
				 				   +'<td width="100" align="center">'+ data.data[i].positionName +'</td>'
				 				   +'<td width="100">'+ data.data[i].companyName +'</td>'
				 				   +'<td width="70">'+ data.data[i].createTime.substring(0,10) +'</td>'
				 				   +'<td width="70">'+ data.data[i].updateTime.substring(0,10) +'</td>'
				 				   +'<td width="70">'+ (data.data[i].userType=="1"?"管理员":"普通用户") +'</td>'
				 				   +'<td width="70">'+ (data.data[i].isActive==1?"有效":"无效") +'</td>'
				 				   +'<td width="175"><div align="center">'
				 				   //+'<a href="#" target="">查看</a>|'
								   +'<a href="javascript:updateUser('+data.data[i].id+',\''+data.data[i].loginName+'\');" target="">修改</a>|'
								   +'<a href="javascript:delUser('+data.data[i].id+');" target="">删除</a>'
				 				   +'</div></td>'
						         +'</tr>';
			}
			htmltxtcontent += '</tbody>';
			 $("#jigou").html(htmltxtHead+htmltxtcontent);
			  //分页器
			if(data.totalPageCount > 1){
				$("#pagingToolDiv").show();
	            $("#pagingToolDiv").html(PagingToolV2.getPagingCode(condition, 'queryUsers', data.totalPageCount,data.totalcount, pageNo));
            }else{
            	$("#pagingToolDiv").hide();
            }
        });
        
}

//跳转到新增用户页面
function toAddUserPage(){
	window.open("/finance/views/console/addUser.jsp","_self");
}

//修改用户
function updateUser(userId,loginName){
	window.open("/finance/views/console/addUser.jsp?userId="+userId+"&loginName='"+loginName+"'","_self");
}

//删除用户
function delUser(id){
	if (confirm("确认删除?该操作会删除与该用户相关所有数据!!!")){
		jQuery.post("/finance/user/delUser.do",{userId:id},function(str){
			var data = eval('(' + str + ')');
			if(data.success){
				jAlert("删除用户成功!","提示");
				queryUsers("",0,"");//刷新界面
			}else{
				jAlert("删除用户失败!","提示");			
			}
	    });
	 }
}

//通过用户名查询用户
function selUsersByName(){
	queryUsers("",0,$("#loginName").val());
}